package cn.joybreeze.controller;


import cn.joybreeze.common.result.R;
import cn.joybreeze.common.vo.RoleQueryVO;
import cn.joybreeze.model.RoleDO;
import cn.joybreeze.service.RoleService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author kjwaa
 * @since 2021-05-23
 */
@RestController
@RequestMapping("/role-do")
public class RoleController {
    @Autowired
    public RoleService roleService;

    /**
     * 增加角色记录
     * @param roleQueryVO
     * @return
     */
    @PostMapping("/add")
    public R addRole(RoleQueryVO roleQueryVO){
        RoleDO roleDO = new RoleDO();
        BeanUtils.copyProperties(roleQueryVO, roleDO);
        boolean isSuccess = roleService.saveOrUpdate(roleDO);
        if(isSuccess) return R.ok();
        return R.error();
    }

    /**
     * 根据id删除角色记录
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public R deleteById(@PathVariable int id){
        boolean isSuccess = roleService.removeById(id);
        if(isSuccess) return R.ok();
        return R.error();
    }

    /**
     * 更新记录
     * @param id
     * @param roleQueryVO
     * @return
     */
    @PostMapping("/update/{id}")
    public R updateRole(@PathVariable int id, @RequestBody RoleQueryVO roleQueryVO){
        RoleDO roleDO = roleService.getById(id);
        BeanUtils.copyProperties(roleQueryVO, roleDO);
        boolean isSuccess = roleService.saveOrUpdate(roleDO);
        if(isSuccess) return R.ok();
        return R.error();
    }

    /**
     * 根据id获取角色记录
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    public R getRoleById(@PathVariable int id){
        RoleDO roleDO = roleService.getById(id);
        if(roleDO == null) return R.error();
        return R.ok(roleDO);
    }

    /**
     * 角色列表
     * @param queryVO
     * @return
     */
    @GetMapping("/list")
    public R getRoleList(RoleQueryVO queryVO){
        Page<RoleDO> page = new Page<>();
        page.setCurrent(queryVO.getCurrent());
        page.setSize(queryVO.getSize());
        Page<RoleDO> doPage = roleService.page(page);
        return R.ok(doPage);
    }





}
